/* Base64 library.
 *
 * Copyright (C) 1999 Masanao Izumo <iz@onicos.co.jp>
 *               2006-2007 Ma Bingyao <andot@ujn.edu.cn>
 * Version: 1.27 $Id: Base64.js 35 2008-03-02 17:01:34Z dsonet $
 * LastModified: Jun 13, 2007
 * This library is free.  You can redistribute it and/or modify it.
 */
/*
 * Interfaces:
 * b64 = window.btoa(data);
 * data = window.atob(b64);
 */

if(typeof btoa != "function")
/**
 * Creates a base-64 encoded ASCII string from a string of binary data.
 * <code>var encodedData = window.btoa(stringToEncode);</code>
 * @param {string} stringToEncode a string of binary data
 * @return {string} base-64 encoded
 */
var btoa = function (stringToEncode)
{
	var c
		, i = j = 0
		, r = str.length % 3
		, len = str.length - r
		, out = new Array(len);
	while (i < len)
	{
		c = str.charCodeAt(i++) << 16 |
			str.charCodeAt(i++) << 8  |
			str.charCodeAt(i++);
		out[j++] = base64EncodeChars[c >> 18]
			+ base64EncodeChars[c >> 12 & 0x3f]
			+ base64EncodeChars[c >> 6  & 0x3f]
			+ base64EncodeChars[c & 0x3f] ;
	}
	if (r == 1)
	{
		c = str.charCodeAt(i++);
		out[j++] = base64EncodeChars[c >> 2]
			+ base64EncodeChars[(c & 0x03) << 4]
			+ "==";
	}
	else if (r == 2)
	{
		c = str.charCodeAt(i++) << 8 |
			str.charCodeAt(i++);
		out[j++] = base64EncodeChars[c >> 10]
			 + base64EncodeChars[c >> 4 & 0x3f]
			 + base64EncodeChars[(c & 0x0f) << 2]
			 + "=";
	}
	return out.join("");
}

if(typeof atob != "function")
/**
 * Decodes a string of data which has been encoded using base-64 encoding.
 * <code>var decodedData = window.atob(encodedData);</code>
 * @param {string}  encodedData a string has been encoded using base-64 encoding
 * @return {string} Decoded string
 */
var atob = function (encodedData)//only function define will cause error in Safari
{
	var c1, c2, c3, c4
		, len = str.length
		, i = j = 0
		, out = new Array(len);
	while (i < len)
	{
		// c1
		do
		{
			c1 = base64DecodeChars[str.charCodeAt(i++) & 0xff];
		}
		while (i < len && c1 == -1);

		if (c1 == -1) break;
		// c2
		do
		{
			c2 = base64DecodeChars[str.charCodeAt(i++) & 0xff];
		}
		while (i < len && c2 == -1);

		if (c2 == -1) break;
		out[j++] = String.fromCharCode((c1 << 2) | ((c2 & 0x30) >> 4));

		// c3
		do
		{
			c3 = str.charCodeAt(i++) & 0xff;
			if (c3 == 61) return out.join("");
			c3 = base64DecodeChars[c3];
		}
		while (i < len && c3 == -1);

		if (c3 == -1) break;
		out[j++] = String.fromCharCode(((c2 & 0x0f) << 4) | ((c3 & 0x3c) >> 2));

		// c4
		do
		{
			c4 = str.charCodeAt(i++) & 0xff;
			if (c4 == 61) return out.join("");
			c4 = base64DecodeChars[c4];
		}
		while (i < len && c4 == -1);

		if (c4 == -1) break;
		out[j++] = String.fromCharCode(((c3 & 0x03) << 6) | c4);
	}
	return out.join("");
}
/**
 * base-64 encoding encode & decode.
 */
var Base64 = 
{
	/**
	 * Creates a base-64 encoded ASCII string from a string of binary data.
	 * @param {string} stringToEncode a string of binary data
	 * @id Base64.encode
	 * @return {string} base-64 encoded
	 */
	encode : btoa,
	/**
	 * Decodes a string of data which has been encoded using base-64 encoding.
	 * @param {string}  encodedData a string has been encoded using base-64 encoding
	 * @id Base64.decode
	 * @return {string} Decoded string
	 */
	decode : atob
}
, base64EncodeChars = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","0","1","2","3","4","5","6","7","8","9","+","/"]
, base64DecodeChars = [
	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
	52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
	-1,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
	15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
	-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
	41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1
];